CS 116: Introduction to Computer Science 2

Tutorial 1 (Solutions): Introduction to Python (Module 1)

Reminders:

  • Assignment 01 is due on Wednesday, May 22nd at 10am

  1. Credit card account numbers often have some security measures encoded within them.

    Write a Python function create_acct_num that consumes a 3-digit account number and returns the corresponding 4-digit number, in which the new last digit is remainder when the sum of the digits of the original number is divided by 7. For example, the check digit for 778 is 1, so the function should return 7781.

    Solution:

    import check
    
    
    def create_acct_num(acct):
        '''
        returns an expanded account number which starts from acct, and appends 
        check_digit (the remainder when the sum of the digits is divided by 7).
        
        create_acct_num: Nat -> Nat
        
        requires: 100 <= acct <= 999
        
        Examples: 
        create_acct_num(123) => 1236
        create_acct_num(778) => 7781
        '''
        ones = acct % 10
        tens = int(acct / 10) % 10
        hundreds = int(acct / 100)
        check_digit = (ones + tens + hundreds) % 7
        new_acct = acct * 10 + check_digit
        return new_acct
    
    # Testing create_account_num:
    check.expect("Test 1", create_acct_num(123), 1236)
    check.expect("Test 2", create_acct_num(778), 7781)
    
    
  2. Write a Python function shipping_charges that calculates cost of shipping boxes. The function consumes a handling charge, the cost per kg for shipping, the weight per box (assumed the same for all boxes), and the number of boxes to ship. The function adds in 13% tax, and returns the total cost. For example, to ship 5 boxes, each 10 kg, when their charges are $0.25 per kg and handling is $10, the total cost is $25.425. Note that the value returned may not stored exactly. So, your test should not check for an exact value, but rather should check that your answer is very close to the expected value.

    Solution:

    import check
    
    
    def shipping_charges(handling, charge_per_kg, weight_per_box, num_boxes):
        '''
        returns the total cost of shipping num_boxes, each weighing weight_per_box kg, 
        at a charge of charge_per_kg. There is a handling change applied, and 
        a tax of 13% is added to everything. 
        
        shipping_charges: 
        (anyof Int Float) (anyof Int Float) (anyof Int Float) Nat -> Float
        
        requires: handling >= 0, charge_per_kg >= 0, weight_per_box >= 0
        
        Example: 
        shipping_charges(10.0,0.25,10.0,5) => 25.425
        shipping_charges(0.0,100.0,88.0,0) => 0
        '''
        total_weight = weight_per_box * num_boxes
        pre_tax = handling + charge_per_kg * total_weight
        tax_rate = 0.13
        total_cost = pre_tax * (1 + tax_rate)
        return total_cost
    
    # Testing shipping_charges:
    check.within("Test 1", shipping_charges(0.0,100.0,88.0,0), 0.0, 0.0001)
    check.within("Test 2", shipping_charges(12.0,0.0,5.0,89098), 13.56, 0.0001)
    check.within("Test 3", shipping_charges(10.0,0.25,10.0,5), 25.425, 0.0001)
    
    
  3. The standard deviation of a set of numbers x_i is calculated as the the square root of the sums (x_i - x_mean)^2, where mean is the arithmetic mean of the x_i values. For example, the standard deviation of 3.2, 9.8, 6.3, 1.2, 3.3 is 3.00039997 Write a Python function std_dev that consumes five floating point numbers (x1,x2,x3,x4,x5) and returns their standard deviation. .

    Solution:

    import check
    import math
    
    
    def sqr_diff(y,z):
        '''
        returns (y-z)^2
        
        sqr_diff: Float Float -> Float
        '''
        return (y-z)**2
    
    
    def std_dev(x1,x2,x3,x4,x5):
        '''
        returns standard deviation of the values x1,x2,x3,x4,x5
    
        std_dev: Float Float Float Float Float -> Float
    
        Example: 
        std_dev(3.2, 9.8, 6.3, 1.2,  3.3) => 3.00039997
        '''
        mean = (x1+x2+x3+x4+x5) / 5
        total_sqrs = sqr_diff(x1,mean) + sqr_diff(x2,mean) + \
            sqr_diff(x3,mean) + sqr_diff(x4,mean) + sqr_diff(x5,mean)
        return math.sqrt(total_sqrs/5)
    
    # Testing
    check.within("sd-typical", std_dev(3.2, 9.8, 6.3, 1.2,  3.3), 3.00039997, 0.00001)
    check.within("sd-0", std_dev(5.8, 5.8, 5.8, 5.8, 5.8), 0.0, 0.00001)
    check.within("mixed values", std_dev(-3.0, 0.0, 7.0, 3.0, -7.0), 4.8166378, 0.00001)
    
    
  4. Consider a basic smart phone plan that charges as follows: * monthly charge $39.00 * $0.50 per phone minutes over 200 free "anytime" minutes * $5.00 for each part of 150 MB of data over the first 100 MB used. For example, if you used 15 minutes of phone calls and 87 MB of data, your charge would be $39.00; if you used 250 minutes of phone calls and 500 MB of data, your charge would be $79.00 ($25.0 extra for the phone calls, and $15.0 extra for data). Write a Python function cell_charge that consumes two Nats (minutes and data) corresponding to the total number of phone minutes used and the amount of data used in a month, and returns the total phone bill for the month, including the basic monthly charge. Do not perform any rounding on the result.

    Solution:

    import check
    import math
    
    
    def cell_charge(minutes, data):
        '''
        returns the monthly charge for a cell plan with a basic charge of $39 
        plus $0.50 per phone minutes used over 200 minutes plus $5 per 150 MB used 
        (or part thereof) over 100 MB in the month
    
        cell_charge: Nat Nat -> Float
    
        Examples: 
        cell_charge(15, 87) => 39.0
        cell_charge(250, 500) => 79.0
        '''
        monthly = 39.0
        extra_minutes = max(0, (minutes-200)) * 0.50
        extra_units = max(0, math.ceil((data-100)/150))
        extra_data = extra_units * 5.0 
        return monthly + extra_minutes + extra_data
    
    # Testing
    check.within("both under", cell_charge(15, 87), 39.0, 0.00001)
    check.within("both at limit", cell_charge(200, 100), 39.0, 0.00001)
    check.within("data 100 over", cell_charge(100, 200), 44.0, 0.00001)
    check.within("data 400 over", cell_charge(100, 500), 54.0, 0.00001)
    check.within("phone 50 over", cell_charge(250, 50), 64.0, 0.00001)
    check.within("phone 50 over and data 400 over", cell_charge(250, 500), 79.0, 0.00001)
    
    

Valid XHTML 1.0 Strict

Last modified on Friday, 20 December 2019, at 14:04.